//Given a string s, return the first non-repeating character in it and return it
//s index. If it does not exist, return -1. 
//
// 
// Example 1: 
// Input: s = "leetcode"
//Output: 0
// Example 2: 
// Input: s = "loveleetcode"
//Output: 2
// Example 3: 
// Input: s = "aabb"
//Output: -1
// 
// 
// Constraints: 
//
// 
// 1 <= s.length <= 105 
// s consists of only lowercase English letters. 
// 
// Related Topics 哈希表 字符串 
// 👍 385 👎 0


package leetcode.editor.cn;

//Java：First Unique Character in a String
class P387FirstUniqueCharacterInAString {
    public static void main(String[] args) {
        Solution solution = new P387FirstUniqueCharacterInAString().new Solution();
        // TO TEST
        solution.firstUniqChar("leetcode");
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int firstUniqChar(String s) {
            int len = s.length();
            if (s == null || s.length() == 0) {
                return -1;
            }
            int[] arr = new int[27];
            for (int i = 0; i < len; i++) {
                int c = s.charAt(i) - 'a';
                if (arr[c] == 0) {
                    arr[c] = i+1;
                } else if (arr[c] != 0) {
                    arr[c] = -1;
                }
            }
            for (int i = 0; i < len; i++) {
                int c = s.charAt(i) - 'a';
                if (arr[c] > 0) {
                    return arr[c] - 1;
                }
            }
            return -1;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}